<%@page import="domain.Contact"%>
<%@page import="domain.ContactGroup"%>
<%@page import="domain.Company"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%>
<%@ taglib prefix="nested" uri="http://struts.apache.org/tags-nested"%>
<!DOCTYPE html>
<html:html>
<head>
<title><bean:message key="updateContact.title" /></title>
<jsp:include page="/pages/header.jsp" />
</head>
<body>
	<div class="container">
		<div class="row">
			<div class="span12">
				<div class="page-header">
					<h1>
						<bean:message key="updateContact.title" />
					</h1>
				</div>
			</div>
		</div>
		<div class="row">
			<jsp:include page="/pages/navbar.jsp" />
			<div class="span10" >
				<% Contact contact = (Contact) request.getAttribute("contact"); request.setAttribute("contact", contact);
	   java.util.List <ContactGroup> books = (java.util.List <ContactGroup>)request.getAttribute("books"); request.setAttribute("books", books);%>
		<h2>Modifier un contact</h2> 
		<html:errors/>
		<html:form action="/updateContact" styleClass="well" styleId="formulaire">
			<%if(contact == null || books == null){%>
				<table id="contact">
				<tr>
					<td><html:hidden property="id"/></td>
				</tr>
				<tr>
					<td><label><bean:message key="firstNameLabel" /></label></td><td><html:text property="firstname"/></td>
				</tr>
				<tr>
					<td><label><bean:message key="lastNameLabel" /></label></td><td><html:text property="lastname"/></td>
				</tr>
				<tr>
					<td><label><bean:message key="emailLabel" /></label></td><td><html:text property="email"/></td>
				</tr>
				<tr>
					<td><label><bean:message key="streetLabel" /></label></td><td><html:text property="street"/></td>
				</tr>
				<tr>
					<td><label><bean:message key="cityLabel" /></label></td><td><html:text property="city"/></td>
				</tr>
				<tr>
					<td><label><bean:message key="zipLabel" /></label></td><td><html:text property="zip"/></td>
				</tr>
				<tr>
					<td><label><bean:message key="countryLabel" /></label></td><td><html:text property="country"/></td>
				</tr>
				<tr><td><label><bean:message key="numPhoneMobileLabel" /></label></td><td><html:text property="numPhoneMobile"/></td></tr>
				<tr><td><label><bean:message key="numPhoneHomeLabel" /></label></td><td><html:text property="numPhoneHome"/></td></tr>
				<tr><td><label><bean:message key="numPhoneDesktopLabel" /></label></td><td><html:text property="numPhoneDesktop"/></td></tr>
				<tr>
				<td><label><bean:message key="booksLabel" /></label></td>
				<td>
				<table id="books">
				<tr>
				<td>
				<button id="btnShowSimple" name="btnShowSimple" class="btn btn-info"><i class="icon-white icon-plus-sign"></i>&nbsp;<bean:message key="addBookButton" /></button>
				</td>
				</tr>
				</table>
				<tr><td><label><bean:message key="companyLabel" /></label></td><td><label class="checkbox"><html:checkbox styleId="company" property="company" value="1"/></label></td></tr>
			</table>								
			<%} else {%>
				
			<table id="contact">
				<tr>
					<td><html:hidden property="id" value="<%=String.valueOf(contact.getId())%>"/></td>
				</tr>
				<tr>
					<td><label><bean:message key="firstNameLabel" /></label></td><td><html:text property="firstname" value="<%=contact.getFirstName()%>"/></td>
				</tr>
				<tr>
					<td><label><bean:message key="lastNameLabel" /></label></td><td><html:text property="lastname" value="<%=contact.getLastName()%>"/></td>
				</tr>
				<tr>
					<td><label><bean:message key="emailLabel" /></label></td><td><html:text property="email" value="<%=contact.getEmail()%>"/></td>
				</tr>
				<tr>
					<td><label><bean:message key="streetLabel" /></label></td><td><html:text property="street" value="<%=contact.getAddress().getStreet()%>"/></td>
				</tr>
				<tr>
					<td><label><bean:message key="cityLabel" /></label></td><td><html:text property="city" value="<%=contact.getAddress().getCity()%>"/></td>
				</tr>
				<tr>
					<td><label><bean:message key="zipLabel" /></label></td><td><html:text property="zip" value="<%=contact.getAddress().getZip()%>"/></td>
				</tr>
				<tr>
					<td><label><bean:message key="countryLabel" /></label></td><td><html:text property="country" value="<%=contact.getAddress().getCountry()%>"/></td>
				</tr>
				<%boolean mobile = false, home = false, desktop = false;
				for(domain.PhoneNumber phone : contact.getPhones()){
					if(phone.getPhoneKind().equals("numPhoneMobile")){
						%><tr><td><label><bean:message key="numPhoneMobileLabel" /></label></td><td><html:text property="numPhoneMobile" value="<%=phone.getPhoneNumber()%>"/></td></tr><%
								mobile = true;	
								break;
					}
				}
				if(!mobile){
					%><tr><td><label><bean:message key="numPhoneMobileLabel" /></label></td><td><html:text property="numPhoneMobile"/></td></tr><%
				}
				for(domain.PhoneNumber phone : contact.getPhones()){
					if(phone.getPhoneKind().equals("numPhoneHome")){
						%><tr><td><label><bean:message key="numPhoneHomeLabel" /></label></td><td><html:text property="numPhoneHome" value="<%=phone.getPhoneNumber()%>"/></td></tr><%
								home = true;	
								break;
					}
				}
				if(!home){
					%><tr><td><label><bean:message key="numPhoneHomeLabel" /></label></td><td><html:text property="numPhoneHome"/></td></tr><%
				}
				for(domain.PhoneNumber phone : contact.getPhones()){
					if(phone.getPhoneKind().equals("numPhoneDesktop")){
						%><tr><td><label><bean:message key="numPhoneDesktopLabel" /></label></td><td><html:text property="numPhoneDesktop" value="<%=phone.getPhoneNumber()%>"/></td></tr><%
								desktop = true;	
								break;
					}
				}
				if(!desktop){
					%><tr><td><label><bean:message key="numPhoneDesktopLabel" /></label></td><td><html:text property="numPhoneDesktop"/></td></tr><%
				}				
				%>
				<tr>
				<td><label><bean:message key="booksLabel" /></label></td>
				<td>
				<table id="books">
				<%for(domain.ContactGroup book : books){
					boolean exists = false;
					for(domain.ContactGroup group : contact.getBooks()){
						if(group.getGroupName().equals(book.getGroupName())){
							exists = true;
							%><tr><td><label class="checkbox"><html:checkbox property="books" value="<%=book.getGroupName()%>" styleClass="toCheck"/><%=book.getGroupName()%></label></td></tr><%
							break;
						}
					}
					if(!exists){
						%><tr><td><label class="checkbox"><html:checkbox property="books" value="<%=book.getGroupName()%>"/><%=book.getGroupName()%></label></td></tr><%
					}
				}%>
				<tr>
				<td>
				<button id="btnShowSimple" name="btnShowSimple" class="btn btn-info"><i class="icon-white icon-plus-sign"></i>&nbsp;<bean:message key="addBookButton" /></button>
				</td>
				</tr>
				</table>
				<%if(contact instanceof Company){%><tr><td>&nbsp;<html:hidden styleId="company" property="company" value="1"/></td></tr><tr><td><tr id="siret"><td>SIRET : </td><td><input id="siret-input" type="text" name="SIRET" value="<%=((Company)contact).getSIRET()%>"/></td></tr></td></tr><%}%>
			</table>	
			<%}%>	
			<br/>		
			<button type="submit" name="submitButton" class="btn btn-success"><i class="icon-white icon-ok-sign"></i>&nbsp;<bean:message key="submitButton" /></button>
			<button type="reset" name="resetButton" class="btn btn-danger" id="resetBtn"><i class="icon-white icon-remove-sign"></i>&nbsp;<bean:message key="resetButton" /></button><br/>
		</html:form>
			</div>
		</div>
		<jsp:include page="/pages/footer.jsp" />
	</div>
</body>
</html:html>